package org.teachertools.web.actions;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.teachertools.data.Competence;
import org.teachertools.data.Eleve;
import org.teachertools.data.Note;
import org.teachertools.services.CompetenceService;
import org.teachertools.services.EleveService;
import org.teachertools.services.NoteService;
import org.teachertools.web.forms.MultipleNoteForm;
import org.teachertools.web.forms.NoteForm;
import org.teachertools.web.forms.NoteFormMapping;

/**
 * @author eveno
 * 
 */
public class NoteMassiveAddAction extends CommonDispatchAction {
    @Override
    protected Map<String, String> getKeyMethodMap() {
        Map<String, String> map = new HashMap<String, String>();
        map.put("button.save", "save");
        map.put("button.massiveadd", "prepareMassiveAdd");
        return map;
    }

    /**
     * 
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public ActionForward prepareMassiveAdd(ActionMapping mapping, ActionForm form, HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        System.err.println("***************** NoteMassiveAddAction.prepareMassiveAdd *****************");
        MultipleNoteForm multipleNoteForm = (MultipleNoteForm) form;

        CompetenceService competenceService = this.getServiceLocator().getCompetenceService();
        EleveService eleveService = this.getServiceLocator().getEleveService();

        Long[] eleveId = multipleNoteForm.getEleve();
        List<Eleve> eleveList = new LinkedList<Eleve>();
        for (Long element : eleveId) {
            Eleve e = eleveService.get(element);
            eleveList.add(e);
        }
        request.setAttribute("ELEVE", eleveList);

        Long[] competenceId = multipleNoteForm.getCompetence();
        List<Competence> competenceList = new LinkedList<Competence>();
        for (Long element : competenceId) {
            Competence c = competenceService.get(element);
            competenceList.add(c);
        }
        request.setAttribute("COMPETENCE", competenceList);

        return mapping.findForward("successPrepareMassiveAdd");
    }

    /**
     * 
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public ActionForward save(ActionMapping mapping, ActionForm form, HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        NoteService noteService = getServiceLocator().getNoteService();
        MultipleNoteForm notes = (MultipleNoteForm) form;

        NoteForm noteForm = new NoteForm();
        // noteForm.setIdCompetence(notes.getCompetenceId());
        noteForm.setDateNote(notes.getDateNote());

        Long[] eleves = notes.getEleve();
        Long[] competences = notes.getCompetence();
        log.info("eleves.length: " + eleves.length);
        log.info("competences.length: " + competences.length);
        for (Long eleve : eleves) {
            noteForm.setId(null);
            noteForm.setEleve(eleve);
            for (Long competence : competences) {
                noteForm.setIdCompetence(competence);
                String note = request.getParameter("note-" + eleve + "-" + competence);
                noteForm.setNote(note);
                String commentaire = request.getParameter("commentaire-" + eleve + "-" + competence);
                noteForm.setCommentaire(commentaire);
                Note newNote = NoteFormMapping.mappingFormToDTO(noteForm, request);
                noteService.save(newNote);
            }
        }

        notes.reset();

        ActionMessages actionMessages = new ActionMessages();
        actionMessages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("success.do", getMessage(request,
                "libelle.lenregistrement")));
        saveMessages(request, actionMessages);

        addCategoryList(request);
        addClassList(request);
        addEleveList(request);

        return mapping.findForward("save");
    }

}
